/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../tools/PacketCreator.h"
#include "PlayerInventory.h"
#include "Transportations.h"
#include "Inventory.h"
#include "Trade.h"
#include "Channel.h"
#include "Player.h"
#include "Item.h"

Trade::Trade(Player* player){
	this->player = player;
	player->setTrade(this);
	trader = 0;
	mesos = 0;
	inv = 0;
	locked = false;
}

void Trade::open() {
	if(trader != 0)
		player->send(PacketCreator().showTrade(player, trader->getPlayer()));
	else
		player->send(PacketCreator().showTrade(player));
}

void Trade::invite(Player* to){
	invited = to;
	inv = to->getChannel()->getTransportations()->getNewInviteID(this);
	to->send(PacketCreator().inviteTrade(player, inv));
}

void Trade::close(){
	player->setMesos(player->getMesos() + mesos);
	for(int i = 0; i < (int)items.size(); i++){
		items[i].second->setSlot(items[i].first);
		player->getInventories()->getInventory(INVENTORY(items[i].second->getID()))->addItem(items[i].second, false);
	}
	player->send(PacketCreator().tradeError(0x0a, 0x02));
	player->setTrade(0);
	if(trader != 0){
		trader->setTrader(0);
		trader->close();
	}
}

void Trade::accept(){
	player->send(PacketCreator().joinTrade(invited));
	invited->setTrade(new Trade(invited));
	trader = invited->getTrade();
	trader->setTrader(this);
	trader->open();
}

void Trade::addMesos(int mesos){
	player->setMesos(player->getMesos() - mesos, true);
	this->mesos += mesos;
	player->send(PacketCreator().tradeMesos(this->mesos, 0));
	if(trader != 0)
		trader->getPlayer()->send(PacketCreator().tradeMesos(this->mesos, 1));
}

void Trade::addItem(Item* item, short slot){
	pair<int, Item*> tItem;
	tItem.first = item->getSlot();
	tItem.second = item;
	items.push_back(tItem);
	item->setSlot(slot);
	player->send(PacketCreator().tradeItem(item, 0));
	if(trader != 0)
		trader->getPlayer()->send(PacketCreator().tradeItem(item, 1));
}

int Trade::getFee(int mesos){
        int fee = 0;
        if (mesos >= 100000000)
            fee = int(0.06 * mesos);
		else if (mesos >= 25000000)
            fee = int(0.05 * mesos);
		else if (mesos >= 10000000)
            fee = int(0.04 * mesos);
		else if (mesos >= 5000000)
            fee = int(0.03 * mesos);
		else if (mesos >= 1000000)
            fee = int(0.018 * mesos);
		else if (mesos >= 100000)
			fee = int(0.008 * mesos);
        return fee;
}

void Trade::complete(){
	if(trader != 0){
		Player* tplayer = trader->getPlayer();
		int tmesos = trader->getMesos();
		vector <pair<int, Item*>>* pitems = trader->getItems();
		bool check = true;
		for(int i=0; i<(int)items.size() && check; i++){
			if(!tplayer->getInventories()->getInventory(INVENTORY(items[i].second->getID()))->addItem(items[i].second, true, false, false))
				check = false;
		}
		for(int i=0; i<(int)pitems->size() && check; i++){
			if(!player->getInventories()->getInventory(INVENTORY((*pitems)[i].second->getID()))->addItem((*pitems)[i].second, true, false, false))
				check = false;
		}
		if(!check){
			player->send(PacketCreator().tradeError(0x0A, 0x08));
			tplayer->send(PacketCreator().tradeError(0x0A, 0x08));
			return;
		}
		int fee = Trade::getFee(tmesos);
		player->send(PacketCreator().tradeError(0x0A, 0x07));
		tplayer->send(PacketCreator().tradeError(0x0A, 0x07));
		player->setMesos(player->getMesos() + tmesos - fee);
		tplayer->setMesos(tplayer->getMesos() + mesos);
		for(int i=0; i<(int)items.size(); i++){
			tplayer->getInventories()->getInventory(INVENTORY(items[i].second->getID()))->addItem(items[i].second);
		}
		for(int i=0; i<(int)pitems->size() ; i++){
			player->getInventories()->getInventory(INVENTORY((*pitems)[i].second->getID()))->addItem((*pitems)[i].second);
		}
	}
}

void Trade::confirm(){
	locked = true;
	trader->getPlayer()->send(PacketCreator().tradeConfirm());
}